package com.ziyun.erp.modules.goods.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.utility.New;
import com.ziyun.erp.modules.goods.dao.GoodsOptionDao;
import com.ziyun.erp.modules.goods.dao.GoodsProductDao;
import com.ziyun.erp.modules.goods.entity.GoodsEntity;
import com.ziyun.erp.modules.goods.entity.GoodsOptionEntity;
import com.ziyun.erp.modules.goods.entity.GoodsProductEntity;
import com.ziyun.erp.modules.goods.service.GoodsProductService;
import com.ziyun.erp.modules.goods.service.GoodsService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("goodsProductService")
public class GoodsProductServiceImpl extends ServiceImpl<GoodsProductDao, GoodsProductEntity> implements GoodsProductService {

    @Autowired
    private GoodsOptionDao goodsOptionDao;

    @Autowired
    private GoodsService goodsService;

    @Override
//    @DataSource(name = DataSourceNames.SECOND)
    public PageUtils queryPage(Map<String, Object> params) {
        Integer goodsId = StringUtil.getRequestParamInteger(params.get("goodsId"));
        Page<GoodsProductEntity> page = this.selectPage(
                new Query<GoodsProductEntity>(params).getPage(),
                new EntityWrapper<GoodsProductEntity>()
                .eq(goodsId != null,"goods_id",goodsId)
        );
        return new PageUtils(page);
    }


    @Override
//    @DataSource(name = DataSourceNames.SECOND)
    //设置goodsProductList,将option对象赋值进入goodsProduct
    public void setOptionByGoodsProductList(List<GoodsProductEntity> goodsProductList) {
        for (int i = 0;i<goodsProductList.size();i++){
            String optionIds = goodsProductList.get(i).getOptionIds();
            //根据optionIds获得option对象
            List<GoodsOptionEntity> goodsOptionList = goodsOptionDao.selectByOptionIds(optionIds);
            goodsProductList.get(i).setGoodsOptionList(goodsOptionList);
        }
    }

    @Override
//    @DataSource(name = DataSourceNames.SECOND)
    public void setCraftOptionByGoodsProductList(List<GoodsProductEntity> goodsProductList) {
        for (int i = 0;i<goodsProductList.size();i++){
            String craftOptionIds = goodsProductList.get(i).getCraftOptionIds();
            //根据optionIds获得option对象
            List<GoodsOptionEntity> goodsCraftOptionList = goodsOptionDao.selectByCraftOptionIds(craftOptionIds);
            goodsProductList.get(i).setGoodsCraftOptionList(goodsCraftOptionList);
        }
    }

    @Override
//    @DataSource(name = DataSourceNames.SECOND)
    public void setCraftValues(GoodsEntity goodsProperty, GoodsProductEntity goodsProductEntity) {
        List<String> strings = new ArrayList<>();
        List<GoodsOptionEntity> goodsOptionEntities = goodsOptionDao.selectByCraftOptionIds(goodsProductEntity.getCraftOptionIds());
        for (int i = 0;i<goodsOptionEntities.size();i++){
            strings.add(goodsOptionEntities.get(i).getOptionValue());
        }
        goodsProperty.setCraftValues(strings);
    }

    @Override
    public List<GoodsProductEntity> selectProductProperties(Map map) {
        return this.baseMapper.selectProductProperties(map);
    }

    @Override
    public String getGoodsTypeByProductId(Long productId) {
        return this.baseMapper.getGoodsTypeByProductId(productId);
    }

    @Override
    public Boolean getMaterialType(Long productId) {
        return this.baseMapper.getMaterialType(productId);
    }

    @Override
    public List<GoodsProductEntity> selectByGoodsId(Long goodsId) {
        Map<String, Object> map = New.hashMap();
        map.put("goods_id",goodsId);
        return this.baseMapper.selectByMap(map);
    }

    @Override
    public GoodsEntity getGoodsAndProduct(Long goodsId) {
        GoodsEntity goodsEntity = goodsService.selectById(goodsId);
        Map<String, Object> map = New.hashMap();
        map.put("goods_id", goodsId);
        List<GoodsProductEntity> goodsProductEntityList = this.baseMapper.selectByMap(map);
        goodsEntity.setGoodsProductList(goodsProductEntityList);
        return goodsEntity;
    }

}
